package util

import (
	"crypto/hmac"
	"hash"
)

// 生成消息认证码
func GenerateHamc(h func() hash.Hash, plainText, key []byte) []byte {
	hash := hmac.New(h, key)
	hash.Write(plainText)
	sum := hash.Sum(nil)
	return sum
}

// 验证消息认证码
func VerifyHamc(h func() hash.Hash, plainText, key, checkMacHash []byte) bool {
	generateHamc := GenerateHamc(h, plainText, key)
	return hmac.Equal(generateHamc, checkMacHash)
}
